using System;
using System.Collections.Generic;
using CKS.EBE.API;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web;
using System.Diagnostics;

namespace CKS.EBE.WebControls
{
	public class ThemeSelector : SPControl
	{
		DropDownList _Themes;
		private string _SelectedTheme;
		private readonly string _CookieName = "ebeBlogTheme-" + SPContext.Current.Web.Url;

		/// <summary>
		/// if true, will show the localized text "Change Theme"
		/// </summary>
		public bool ShowLiteral;

		/// <summary>
		/// Get/Set selected theme in cookie
		/// </summary>
		public string SelectedTheme
		{
			get
			{
				if (Page.Request.Browser.Cookies)
				{
					//Cookies Supported, try to get the cookie
					HttpCookie oCookie = Page.Request.Cookies[_CookieName];

					// use cookie value for theme. if not available, the blog theme is used
					_SelectedTheme = oCookie == null || string.IsNullOrEmpty(oCookie.Value) ? BlogContext.Current.BlogTheme : oCookie.Value;
				}
				else
				{
					// cookies not supported. have to store the selected theme in the session
					object sessionStore = Page.Session[_CookieName];
					_SelectedTheme = sessionStore == null ? BlogContext.Current.BlogTheme : sessionStore.ToString();
				}

				// apply the selected theme to the BlogContext
				BlogContext.SelectedBlogTheme = _SelectedTheme;
				return _SelectedTheme;
			}
			set
			{
				_SelectedTheme = value;
				if (Page.Request.Browser.Cookies)
				{
					// try to get the cookie
					HttpCookie oCookie = Page.Request.Cookies[_CookieName];
					// create it if it doesnt exist
					if (oCookie == null)
						oCookie = new HttpCookie(_CookieName, value);
					else
						oCookie.Value = value;

					oCookie.Expires = DateTime.Now.AddYears(2);
					// write cookie to response, so the client receives it
					Page.Response.Cookies.Add(oCookie);
				}
				else
				{
					// cookies not suported. store selected theme in session
					object sessionStore = Page.Session[_CookieName];
					if (sessionStore == null)
						Page.Session.Add(_CookieName, value);
					else
						Page.Session[_CookieName] = value;
				}

				// apply the selected theme to the BlogContext
				BlogContext.SelectedBlogTheme = value;
			}
		}

		protected override void OnInit(EventArgs e)
		{
			EnsureChildControls();

			base.OnInit(e);
		}

		protected override void CreateChildControls()
		{
			try
			{
				if (!BlogContext.Current.Settings.ThemeSelectorEnabled) return;

				if (ShowLiteral)
				{
					var literal = new Literal { ID = "litThemeSelector", Text = Localization.GetResource("ChangeTheme") + "&nbsp;" };
					Controls.Add(literal);
				}

				List<string> availableThemes;
				if (BlogContext.Current.Settings.UseSiteCollectionThemes)
				{
					availableThemes = Themes.GetThemes(SPContext.Current.Site.RootWeb);
				}
				else
				{
					availableThemes = Themes.GetThemes(SPContext.Current.Web);
				}
				_Themes = new DropDownList { ID = "ThemeSelector", AutoPostBack = true, DataSource = availableThemes };
				_Themes.SelectedIndexChanged += dl_SelectedIndexChanged;
				_Themes.DataBind();
				_Themes.SelectedValue = SelectedTheme;

				Controls.Add(_Themes);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex);
			}

			//base.CreateChildControls();
		}

		void dl_SelectedIndexChanged(object sender, EventArgs e)
		{
			SelectedTheme = _Themes.SelectedValue;

			Page.Response.Redirect(Page.Request.Url.ToString());
		}
	}
}